package com.correo.vista.client.windows.ventanasInternas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.correo.database.beans.UserBean;
import com.correo.socket.client.ClientSocketCorreo;
import com.correo.socket.client.beans.UsuarioValidado;
import com.correo.vista.client.windows.Correo;

public class ConnectView extends JDialog{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton 			buttonConnect;
    private JComboBox 			comboBoxHost;   
    private JLabel 				labelHost;
    private JLabel 				labelPassword;
    private JLabel 				labelPort;
    private JLabel 				labelUser;
    private JPasswordField 		passwordFieldPasswprd;
    private JTextField 			textFieldPort;
    private JTextField 			textFieldUser;
	private Correo 				correo;
	
	public ConnectView(JFrame frame,Correo correo, boolean modal){
		super(frame,"Connect",true);//iconifiable
		this.correo = correo;
		initComponent();
		initVentana();
	}
	private void initVentana() {
		//pack();
		//setSize(190, 380);
		//setPreferredSize(new Dimension(200,200));
		//setVisible(true);
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-190)/2, (screenSize.height-270)/2, 190, 270);
        setResizable(false);
		
		
	}
	private void newComponents(){
        labelHost = new javax.swing.JLabel();
        labelPort = new javax.swing.JLabel();
        textFieldPort = new javax.swing.JTextField();
        labelUser = new javax.swing.JLabel();
        textFieldUser = new javax.swing.JTextField();
        labelPassword = new javax.swing.JLabel();
        comboBoxHost = new javax.swing.JComboBox();
        buttonConnect = new javax.swing.JButton();
        passwordFieldPasswprd = new javax.swing.JPasswordField();
		
	}
	private void initComponent(){
		newComponents();
		//setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        //setClosable(true);
        
        setTitle("Connect");
        setName("Connect");
        //internalFrameConnect.setVisible(true);

        labelHost.setText("Host:");
        //labelHost.setName("Host");

        labelPort.setText("Port:");
        //labelPort.setName("labelPort");

        textFieldPort.setText("10200");
        //textFieldPort.setName("textFieldPort");

        labelUser.setText("User:");
        //labelUser.setName("labelUser");

        textFieldUser.setText("");
        //textFieldUser.setName("textFieldUser");

        labelPassword.setText("Password:");
        //labelPassword.setName("labelPassword");

        //comboBoxHost.setName("comboBoxHost");
        comboBoxHost.setEditable(true);
       ((DefaultComboBoxModel)comboBoxHost.getModel()).addElement("localhost");
        
        buttonConnect.setText("Connect");
       // buttonConnect.setName("buttonConnect");

        passwordFieldPasswprd.setText("");
        //passwordFieldPasswprd.setName("passwordFieldPasswprd");

        javax.swing.GroupLayout internalFrameConnectLayout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(internalFrameConnectLayout);
        internalFrameConnectLayout.setHorizontalGroup(
            internalFrameConnectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(internalFrameConnectLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(internalFrameConnectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordFieldPasswprd, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, internalFrameConnectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(buttonConnect)
                        .addGroup(internalFrameConnectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(labelHost)
                            .addComponent(labelPort)
                            .addComponent(labelUser)
                            .addComponent(labelPassword)
                            .addComponent(comboBoxHost, 0, 138, Short.MAX_VALUE)
                            .addComponent(textFieldPort)
                            .addComponent(textFieldUser))))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        internalFrameConnectLayout.setVerticalGroup(
            internalFrameConnectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(internalFrameConnectLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelHost)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboBoxHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordFieldPasswprd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonConnect)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        setActionListener();
        
	}
	private void setActionListener() {
		buttonConnect.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				UserBean user = new UserBean();
				ClientSocketCorreo clienteSocket =new ClientSocketCorreo((String)comboBoxHost.getSelectedItem(),Integer.parseInt(textFieldPort.getText()),textFieldUser.getText(),new String(passwordFieldPasswprd.getPassword()));
				UsuarioValidado usuarioValidado = clienteSocket.enviar();
				if (usuarioValidado != null)
				{
				user.setId(usuarioValidado.getUserName());
				user.setPasword(usuarioValidado.getPassword());
				correo.setUserConnect(user);
				correo.getModelClient().setConnection(usuarioValidado);
				setVisible(false);
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Datos no validos, intente de nuevo","Error de logueo",0);
				}
				
				
			}
			
		});
		
	}
}
